package com.example.modulepicselector;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import android.view.View;

import com.example.modulepicselector.adapter.PicAdapter;
import com.example.modulepicselector.internal.entity.Item;
import com.example.modulepicselector.internal.entity.SelectionSpec;

import com.example.modulepicselector.internal.model.PicCollection;
import com.example.modulepicselector.view.activity.PagerActivity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import static android.widget.NumberPicker.OnScrollListener.SCROLL_STATE_IDLE;

public class MatActivity extends AppCompatActivity implements PicCollection.PicCallbacks, PicAdapter.OnRecyclerViewItemClickListener {
    private RecyclerView mRvListPic;


    private final PicCollection mPicCollection = new PicCollection();
    private SelectionSpec mSpec;
    private PicAdapter mPicAdapter;
    public static boolean sIsViewIdle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mat);
        initView();
        mSpec = SelectionSpec.getInstance();
        mPicAdapter = new PicAdapter(this,mRvListPic);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        mRvListPic.setLayoutManager(gridLayoutManager);
        mPicAdapter.setOnItemClickListener(this);

        mRvListPic.setAdapter(mPicAdapter);
        mPicCollection.onCreate(this,this);
        mPicCollection.onRestoreInstanceState(savedInstanceState);
        mPicCollection.loadPic();
        mRvListPic.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull @NotNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == SCROLL_STATE_IDLE){
                    sIsViewIdle = true;
                    mPicAdapter.notifyDataSetChanged();
                }else {
                    sIsViewIdle = false;
                }
            }
        });
    }

    private void initView(){

        mRvListPic = findViewById(R.id.rv_pic_list);


    }


    @Override
    public void onPicLoad(Cursor cursor) {

        mPicAdapter.swapCursor(cursor);
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(() -> {

        });
    }

    @Override
    public void onPicReset() {

    }


    @Override
    public void onItemClick(List<Item>selectItems) {
        Intent intent = new Intent(this, PagerActivity.class);
        intent.putParcelableArrayListExtra("select_items", (ArrayList<? extends Parcelable>) selectItems);
        startActivityForResult(intent,12);
    }
}